//
//  ADViewController.m
//  iStore
//
//  Created by Geek on 15/4/22.
//  Copyright (c) 2015年 mirrtalk. All rights reserved.
//

#import "ADViewController.h"
#import "RecordHeadViewController.h"
#import "RecordTailViewController.h"
#import "LDProgressView.h"
#import <AVFoundation/AVFoundation.h>

@interface ADViewController () <AVAudioPlayerDelegate>

{
    // 滑动条
    LDProgressView *_progressView;
    LDProgressView *_proView;
    BOOL _click;
    BOOL _selected;
    
    // 音频对象
    AVAudioPlayer *_player;
    AVAudioPlayer *_trailerPlyer;
    
}

@property (weak, nonatomic) IBOutlet UIView *MyView;
@property (weak, nonatomic) IBOutlet UIView *MYView;

@end

@implementation ADViewController

- (void)viewWillAppear:(BOOL)animated
{
    [_singBtn setTitle:@"试听" forState:UIControlStateNormal];
    [_record setTitle:@"试听" forState:UIControlStateNormal];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"类型选择";
    self.navigationItem.leftBarButtonItem.title = @"返回";
    
    _selected = YES;
    _click = YES;
    
    [_record setBackgroundImage:[UIImage imageNamed:@"Rectangle_6"] forState:UIControlStateHighlighted];
    [_singBtn setBackgroundImage:[UIImage imageNamed:@"Rectangle_6"] forState:UIControlStateHighlighted];
    [_recordButton setBackgroundImage:[UIImage imageNamed:@"Rectangle_6"] forState:UIControlStateHighlighted];
    [_singButton setBackgroundImage:[UIImage imageNamed:@"Rectangle_6"] forState:UIControlStateHighlighted];
    
    _progressView = [[LDProgressView alloc] initWithFrame:CGRectMake(10, 80,[UIScreen mainScreen].bounds.size.width-160, 20)];
    _progressView.color = [UIColor colorWithRed:0.02f green:0.74f blue:0.03f alpha:1.00f];
    _progressView.flat = @YES;
    _progressView.animate = @YES;
    _progressView.borderRadius = @5;
    _progressView.progress = 0;
    [_MyView addSubview:_progressView];
    
    _proView = [[LDProgressView alloc] initWithFrame:CGRectMake(10, 80,[UIScreen mainScreen].bounds.size.width- 160, 20)];
    _proView.color = [UIColor colorWithRed:0.02f green:0.74f blue:0.03f alpha:1.00f];
    _proView.flat = @YES;
    _proView.animate = @YES;
    _proView.borderRadius = @5;
    _proView.progress = 0;
    [_MYView addSubview:_proView];
    
    
    //初始化播放器的时候如下设置
    UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
                            sizeof(sessionCategory),
                            &sessionCategory);
    
    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
                             sizeof (audioRouteOverride),
                             &audioRouteOverride);
    
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    //默认情况下扬声器播放
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
    [audioSession setActive:YES error:nil];
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    [_singBtn  setTitle:@"试听" forState:UIControlStateNormal];
    [_progressView stopTime];
    _selected = YES;
    
    [_record setTitle:@"试听" forState:UIControlStateNormal];
    [_proView stopTime];
    _click = YES;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.wdw
}

- (IBAction)auditionBtnClick:(UIButton *)sender {
    
    if (sender.tag == 101) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"record2" ofType:@"mp3"];
        
        if (path ==  nil) {
            alert(@"你播放的文件不存在");
        }else{
            NSURL *url = [NSURL fileURLWithPath:path];
            
            _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
            _player.delegate = self;
            
            if (_selected) {
                
                [_progressView setProgress:1 withTime:_player.duration];
                
                _selected = !_selected;
                
                [sender setTitle:@"停止" forState:UIControlStateNormal];
                
                [ _player play];
                
                //尾标停止
                [_trailerPlyer stop];
                [_record setTitle:@"试听" forState:UIControlStateNormal];
                
                [_proView stopTime];
                
                
                return;
            }else{
                
                
                [_progressView stopTime];
                
                _selected = !_selected;
                [sender setTitle:@"试听" forState:UIControlStateNormal];
                [_player stop];
                
                return;
            }
        }
        
    }else if (sender.tag == 102){
        
        NSString *path = [[NSBundle mainBundle]pathForResource:@"record1" ofType:@"mp3"];
        if (path ==  nil) {
            alert(@"你播放的文件不存在");
        }else{
            
            NSURL *url = [NSURL fileURLWithPath:path];
            
            _trailerPlyer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
            _trailerPlyer.delegate = self;
            
            
            if (_click) {
                
                [_proView setProgress:1 withTime:_trailerPlyer.duration];
                
                _click= !_click;
                
                [sender setTitle:@"停止" forState:UIControlStateNormal];
                
                [_trailerPlyer play];
                
                [_player stop];
                [_singBtn setTitle:@"试听" forState:UIControlStateNormal];
                
                [_progressView stopTime];
                return;
            }else{
                
                [_proView stopTime];
                _click = !_click;
                
                [sender setTitle:@"试听" forState:UIControlStateNormal];
                
                [_trailerPlyer stop];
                
                return;
            }
        }
    }
}
- (IBAction)chooseBtnClick:(UIButton *)sender {
    
    if (sender.tag == 101) {
        [_trailerPlyer stop];
        [_player stop];
        [_proView setProgress:0 withTime:0];
        [_progressView setProgress:0 withTime:0];
        RecordHeadViewController * hVC = [[RecordHeadViewController alloc]init];
        [self.navigationController pushViewController:hVC animated:YES];
        
    }if (sender.tag == 102) {
        [_trailerPlyer stop];
        [_player stop];
        [_proView setProgress:0 withTime:0];
        [_progressView setProgress:0 withTime:0];
        RecordTailViewController * tVC = [[RecordTailViewController alloc]init];
        [self.navigationController pushViewController: tVC animated:YES];
        
    }
    
}
@end
